import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Observable, forkJoin } from 'rxjs';

@Component({
  selector: 'app-fork-join-use',
  templateUrl: './fork-join-use.component.html',
  styleUrls: ['./fork-join-use.component.less'],
})
export class ForkJoinUseComponent implements OnInit {
  dataList = [];
  constructor(private httpClient: HttpClient) {}

  ngOnInit(): void {}
  getData(url, params): Observable<any> {
    return this.httpClient.get(url, { params });
  }

  getDataList() {
    const url = 'http://localhost:8080/api/mockDataNodePaging';
    const params1 = { page: 1, size: 5 };
    const params2 = { page: 2, size: 3 };
    this.dataList = [];
    forkJoin([
      this.getData(url, params1),
      this.getData(url, params2),
    ]).subscribe(
      ([resp1, resp2]: any) => {
        if (resp1.code === 200 && resp2.code === 200) {
          const res = [...resp1.result.data, ...resp2.result.data];
          this.dataList = res;
        } else {
          alert('接口获取失败');
        }
      },
      (error) => {
        alert('接口获取失败');
      }
    );
  }
}
